const path = require("path");
const protobuf = require("protobufjs");
const fs = require("fs");
// const glob = require('glob');
// const paths = glob.sync('../packages/*/src/pb');
// console.log(process.argv);
const args = process.argv.slice(2); 
// console.log(args); 

const protoDir = path.resolve(__dirname, `../packages/${args}/src/pb`);
const outputDir = path.resolve(__dirname, `../packages/${args}/src/pb`);

if (!fs.existsSync(outputDir)) {
  fs.mkdirSync(outputDir, { recursive: true });
}

fs.readdirSync(protoDir).forEach((file) => {
  if (file.endsWith(".proto")) {
    const filePath = path.join(protoDir, file);
    protobuf.load(filePath, (err, root) => {
      if (err) {
        console.error(`Failed to parse ${file}:`, err);
        return;
      }
      const outputFilePath = path.join(outputDir, `${path.basename(file, ".proto")}.json`);
      fs.writeFileSync(outputFilePath, JSON.stringify(root.toJSON(), null, 2));
      console.log(`Parsed ${file} -> ${outputFilePath}`);
    });
  }
});
